package com.han.hotelplat.cms.controller.datacenter.attendance;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import com.alibaba.fastjson.JSONObject;
import com.han.hotelplat.comm.ResponseWrapTo;
import com.han.hotelplat.comm.utils.HssfUtil;
import com.han.hotelplat.model.AttendanceModel;
import com.han.hotelplat.service.datacenter.attendance.AttendanceService;
/**
 * 
	 ***************************************************************
	 * <p>
	 * @CLASS				:	CMSAttendanceController
	 * @DESCRIPTION	:	
	 * @AUTHOR				:	han1088
	 * @VERSION			:	v1.0
	 * @DATE					:	2018年11月29日 下午5:36:17            
	 * </p>
	 * 
	 * <p>
	 * @MODIFY	LOG	:		
	 * @AUTHOR				:	han1088
	 * @VERSION			:	v1.0
	 * @MODIFY DATE	:	
	 * </p>    
	 ****************************************************************
 */
@Controller
@RequestMapping("/cms/datacenter/attendance")
public class CMSAttendanceController {
	@Autowired
	private AttendanceService attendanceService;
	
	/**
	 * 考勤概述
		 * @description	
		 * @author  han1088
		 * @param startTime 起始时间
		 * @param endTime 终止时间
		 * @param type 时间跨度 0 当日，1本周，2本月
		 * @return
	 */
	@ResponseBody
	@RequestMapping(value = "getAttendanceTotal", produces = "application/json;charset=UTF-8", method = { RequestMethod.GET, RequestMethod.POST })
	public String getAttendanceTotal(String startTime, String endTime, Integer type, Integer groupArea){
		if(StringUtils.isBlank(startTime) && StringUtils.isBlank(endTime) && (type == null || type.intValue() < 0)){
			return new ResponseWrapTo(-1, "请输入要查询的时间段", null).toJson();
		}
		Map<String,Object> data = attendanceService.getAttendanceTotal(startTime, endTime, type);
		attendanceService.getAttendanceGroupByArea(startTime, endTime, type, data, groupArea);
		return new ResponseWrapTo(0, "数据获取成功", data).toJson();
	}
	
	/**
	 * 考勤概述
		 * @description	
		 * @author  han1088
		 * @param startTime 起始时间
		 * @param endTime 终止时间
		 * @param type 时间跨度 0 过去7天，1过去30天
		 * @return
	 */
	@ResponseBody
	@RequestMapping(value = "getAttendanceByPrice", produces = "application/json;charset=UTF-8", method = { RequestMethod.GET, RequestMethod.POST })
	public String getAttendanceByPrice(String startTime, String endTime, Integer type, Integer areaCode){
		if(StringUtils.isBlank(startTime) && StringUtils.isBlank(endTime) && (type == null || type.intValue() < 0)){
			return new ResponseWrapTo(-1, "请输入要查询的时间段", null).toJson();
		}
		Map<String,Object> data  = attendanceService.getAttendanceGroupByDate(startTime, endTime, type, areaCode);
		return new ResponseWrapTo(0, "数据获取成功", data).toJson();
	}
	
	/**
	 * 考勤查询
		 * @description	
		 * @author  han1088
		 * @param startTime 起始时间
		 * @param endTime 终止时间
		 * @param sortType 分组类型1按日期，0按酒店
		 * @return
	 */
	@ResponseBody
	@RequestMapping(value = "searchAttendanceByPrice", produces = "application/json;charset=UTF-8", method = { RequestMethod.GET, RequestMethod.POST })
	public String searchAttendanceByPrice(String startTime, String endTime, Integer areaCode, String price, Integer sortType){
		Map<String,Object> data = new HashMap<String, Object>();
		if(sortType != null && sortType.intValue() == 1){//按照日期归类
			data  = attendanceService.searchAttendanceGroupByDate(startTime, endTime, areaCode, price, sortType);
		}else {//按照酒店归类
			data  = attendanceService.searchAttendanceGroupByHotel(startTime, endTime, areaCode, price, sortType);
		}
		return new ResponseWrapTo(0, "数据获取成功", data).toJson();
	}
	
	/**
	 * 导出考勤数据报表
		 * @description	
		 * @author  han1088
		 * @param startTime 起始时间
		 * @param endTime 终止时间
		 * @param type 时间跨度 0 过去7天，1过去30天
		 * @return
	 */
	@ResponseBody
	@RequestMapping(value = "exportAttendanceGroupDate", produces = "application/json;charset=UTF-8", method = { RequestMethod.GET, RequestMethod.POST })
	public void exportAttendanceGroupDate(HttpServletResponse resp, HttpServletRequest req, String startTime, String endTime, Integer type, Integer areaCode){
		Map<String,Object> re  = attendanceService.getAttendanceGroupByDate(startTime, endTime, type, areaCode);
		List<JSONObject> data = (List<JSONObject>) re.get("data");
		String filename = "考勤数据.xls";
		resp.setHeader("Content-Type", "application/octet-stream");
		try {
			resp.setHeader("Content-Disposition", "attachment;filename=" + HssfUtil.buildAttachmentFileName(req, filename));
			List<Object[]> datas = new ArrayList<Object[]>();
			String[] columnNames = new String[] { "日期","提交房量","确认房量","提交人次","酒店总量","日均房量","人均房量"};
			for(JSONObject d : data){
				Object[] dataObj = new Object[columnNames.length];
				dataObj[0] = d.get("date");
				JSONObject attendance = d.getJSONObject("attendance");
				dataObj[1] = attendance.get("sumRoomCount");
				dataObj[2] = attendance.get("authRoomCount");
				dataObj[3] = attendance.get("userCount");
				dataObj[4] = attendance.get("hotelCount");
				dataObj[5] = attendance.get("avgDayRoomcount");
				dataObj[6] = attendance.get("avgUserRoomcount");
				datas.add(dataObj);
			}
			HssfUtil.writeHssf("考勤数据", columnNames, datas, filename, resp.getOutputStream());
		}catch(Exception e){
			e.printStackTrace();
		}
	}
	
	/**
	 * 导出考勤查询报表
		 * @description	
		 * @author  han1088
		 * @param startTime 起始时间
		 * @param endTime 终止时间
		 * @param sortType 分组类型1按日期，0按酒店
		 * @return
	 */
	@ResponseBody
	@RequestMapping(value = "exportAttendanceByPrice", produces = "application/json;charset=UTF-8", method = { RequestMethod.GET, RequestMethod.POST })
	public void exportAttendanceByPrice(HttpServletResponse resp, HttpServletRequest req, String startTime, String endTime, Integer areaCode, String price, Integer sortType){
		Map<String,Object> data = new HashMap<String, Object>();
		if(sortType != null && sortType.intValue() == 1){//按照日期归类
			data  = attendanceService.searchAttendanceGroupByDate(startTime, endTime, areaCode, price, sortType);
			exportAttendanceGroupDate(resp, req,data);
		}else {//按照酒店归类
			data  = attendanceService.searchAttendanceGroupByHotel(startTime, endTime, areaCode, price, sortType);
			exportAttendanceGroupHotel(resp, req,data);
		}
	}

	private void exportAttendanceGroupDate(HttpServletResponse resp,
			HttpServletRequest req, Map<String, Object> data) {
		String filename = "考勤数据.xls";
		resp.setHeader("Content-Type", "application/octet-stream");
		try {
			resp.setHeader("Content-Disposition", "attachment;filename=" + HssfUtil.buildAttachmentFileName(req, filename));
			List<Object[]> datas = new ArrayList<Object[]>();
			String[] columnNames = new String[] { "日期","预计提交人次","提交人次","已提交房量","已确认房量","提交率","人日均"};
			List<AttendanceModel> attain = (List<AttendanceModel>) data.get("data");
			for(AttendanceModel d : attain){
				Object[] dataObj = new Object[columnNames.length];
				dataObj[0] = d.getCreatTime();
				dataObj[1] = d.getUsercount();
				dataObj[2] = d.getSubmitUserCount();
				dataObj[3] = d.getRoomcount();
				dataObj[4] = d.getAuthRoomcount();
				dataObj[6] = d.getSubmitRatio();
				dataObj[5] = d.getAvgUserRoomCount();
				datas.add(dataObj);
			}
			HssfUtil.writeHssf("考勤数据", columnNames, datas, filename, resp.getOutputStream());
		}catch(Exception e){
			e.printStackTrace();
		}
	}
	
	private void exportAttendanceGroupHotel(HttpServletResponse resp,
			HttpServletRequest req, Map<String, Object> data) {
		String filename = "考勤数据.xls";
		resp.setHeader("Content-Type", "application/octet-stream");
		try {
			resp.setHeader("Content-Disposition", "attachment;filename=" + HssfUtil.buildAttachmentFileName(req, filename));
			List<Object[]> datas = new ArrayList<Object[]>();
			String[] columnNames = new String[] { "酒店名称","预计提交人次","提交人次","已提交房量","已确认房量","提交率","人日均","日均"};
			List<AttendanceModel> attain = (List<AttendanceModel>) data.get("data");
			for(AttendanceModel d : attain){
				Object[] dataObj = new Object[columnNames.length];
				dataObj[0] = d.getHotelname();
				dataObj[1] = d.getUsercount();
				dataObj[2] = d.getSubmitUserCount();
				dataObj[3] = d.getRoomcount();
				dataObj[4] = d.getAuthRoomcount();
				dataObj[5] = d.getSubmitRatio();
				dataObj[6] = d.getAvgUserRoomCount();
				dataObj[7] = d.getAvgDayRoomCount();
				datas.add(dataObj);
			}
			HssfUtil.writeHssf("考勤数据", columnNames, datas, filename, resp.getOutputStream());
		}catch(Exception e){
			e.printStackTrace();
		}
	}
}
